/**
 * Copyright 2010 Tristan Slominski
 *
 * Licensed under the MIT License (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.opensource.org/licenses/mit-license.php
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.mindash.handlebars.client.services.utils.impl;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.google.gwt.http.client.URL;
import com.google.gwt.user.client.Window;
import com.mindash.handlebars.client.services.utils.PlaceServiceUtils;

/**
 * @author Tristan Slominski
 * 
 */
public class PlaceServiceUtilsImpl implements PlaceServiceUtils {
 
  @Override
  public String getPlaceName(String historyToken) {
    String result = "";

    // if no history token return empty string
    if (historyToken == "") {
      return result;
    }

    int questionMarkIndex = historyToken.indexOf("?");
    if (questionMarkIndex > -1) {
      result = historyToken.substring(0, questionMarkIndex);
    } else {
      result = historyToken;
    }

    return result;
  }

  @Override
  public Map<String, String> parseUrlParams(String historyToken) {
    Map<String, String> params = new HashMap<String, String>();

    // if no params, return empty params
    if (Window.Location.getParameterMap().isEmpty()
        && (historyToken == null || !historyToken.contains("?"))) {
      return params;
    }

    // get params from the URL before '#'
    Map<String, List<String>> urlParams = Window.Location.getParameterMap();
    for (Entry<String, List<String>> urlParam : urlParams.entrySet()) {
      params.put(URL.decode(urlParam.getKey()), URL.decode(urlParam.getValue()
          .get(0)));
    }

    // get params from the historyToken, after '#'
    if (historyToken.contains("?")) {
      int questionMarkIndex = historyToken.indexOf("?") + 1;

      // get the substring of params var=1&var2=2&var3=3...
      String[] paramArr =
          historyToken.substring(questionMarkIndex, historyToken.length())
              .split("&");

      for (int i = 0; i < paramArr.length; i++) {
        String[] param = paramArr[i].split("=");
        params.put(URL.decode(param[0]), URL.decode(param[1]));
      }
    }

    return params;
  }

  @Override
  public String paramsToUrlString(Map<String, String> params) {
    String result = "";

    // empty string if no params
    if (params.isEmpty()) {
      return result;
    }

    result = result + "?";

    // assemble the string
    Iterator<Entry<String, String>> i = params.entrySet().iterator();
    while (i.hasNext()) {
      Entry<String, String> entry = i.next();
      result =
          result + URL.encode(entry.getKey()) + "="
              + URL.encode(entry.getValue());
      if (i.hasNext()) {
        result = result + "&";
      }
    }

    return result;

  }

}
